package com.share.servlet;

import lombok.SneakyThrows;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
一、ServletContext：
    1、域对象，在一定范围内（当前应用），使多个Servlet共享数据
    2、获取全局配置信息，与ServletConfig的区别是，ServletConfig只能获取当前<servlet>的配置信息
    3、获取资源路径
 */
public class ServletContext2 extends HttpServlet {


    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Thread.sleep(5000);
        ServletContext application = getServletContext();
        String name = (String)application.getAttribute("name");
        if(null == name) {
            System.out.println("ServletContext 中没有该key");
        }
        System.out.println("name:" + name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
